package dataBase;
/*****************************************************************************
 * 파일이름 : DBManager.java
 * 파일내용 : DB에서 Connection Pool을 관리한다.(Singleton Design Pattern)
 * 작 성 자  : Soo-bae. Park.
*****************************************************************************/

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import snaq.db.ConnectionPool;

public class DBManager
{ 
	public static DBManager dbm = new DBManager();
	private static ConnectionPool pool;
	public static Connection tempCon;
	public static Statement tempStmt;

	/**
	 * 내용		: private Constructor
	 * 입력 값	:
	 * 출력 값	:
	 */
	private DBManager()
	{
		String driver = "com.mysql.jdbc.Driver";
		String poolName = "DB_POOL";
		int maxPool = 10;
		int maxConnection = 30;
		int maxTime = 180000;
		String dbUrl = "jdbc:mysql://localhost:3306/immortal";
		String userId = "demili53";
		String userPass = "ktm999";
		


		System.out.println("### NEW DB Connection Pool Creation ##########################################");

		System.out.println("..... POOL_NAME = " + poolName);
		System.out.println("..... MAX_POOL = " + maxPool);
		System.out.println("..... MAX_CONNECTION = " + maxConnection);
		System.out.println("..... CONNECTION_TIME = " + maxTime);
		System.out.println("..... URL = " + dbUrl);
		System.out.println("..... ID = " + userId);
		System.out.println("..... PASS = " + userPass);

		try
		{
			Class.forName(driver);
		}
		catch(ClassNotFoundException cnf)
		{
			cnf.printStackTrace();
		}
		pool = new ConnectionPool(poolName, maxPool, maxConnection, maxTime, dbUrl, userId, userPass);

		System.out.println("##############################################################################\n");
	}

	/**
	 * 내용		: ConnectionPool에서 Connection객체를 하나 return한다.
	 * 입력 값	:
	 * 출력 값	: Connection confListKeyList
	 */
	public static Connection getConnection() throws SQLException
	{
		return pool.getConnection();
	}
	/*
	 * 메니져 얻기
	 */
	public static DBManager getDbm()
	{
		return dbm;
	}
	/*
	 * 스테이트먼트 얻기
	 */
	public static Statement createStatement(){
		try {
			tempCon = DBManager.getConnection();
			tempStmt= tempCon.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tempStmt;
		
	}
	/*
	 * 배치 add
	 */
	public static void addBatch(String sql){
		try {
			tempStmt.addBatch(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/*
	 * executeBatch();
	 */
	public static int[] executeBatch(){
		int[] result=null;
		try {
			result=tempStmt.executeBatch();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
		
	}
	/*
	 * 커넥션 닫기
	 */
	public static void closeStmt(){
		try {
			tempStmt.close();
			tempCon.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/*
	 * 한개의 값 가져오기
	 */
	public HashMap getOneData(String sql){
		ArrayList tempList;
		HashMap tempMap;
		tempList=getQueryDataArayList(sql);
		tempMap=(HashMap)tempList.get(0);
		return tempMap;
	}
	/*
	 * 때밀이 추가2011 8 16
	 */
	public  ArrayList getQueryDataArayList(String query)
	{
		System.out.println(query);
		ArrayList tempArrayList=new ArrayList();//리턴 벨류
		try
		{
			Connection con = DBManager.getConnection();
			Statement stmt= con.createStatement();

			ResultSet rset = stmt.executeQuery(query);
			if(rset==null)
			{
				System.out.println("rset =Null");
				return null;
			}
			ResultSetMetaData rsmd=rset.getMetaData();
			int coluneNum=rsmd.getColumnCount();
			int count=0;
			while(rset.next())
			{
				count=count+1;
				HashMap tempResult=new HashMap();
				for(int i =1;i<=coluneNum;i++)
				{
					tempResult.put(rsmd.getColumnName(i), rset.getString(i));
				}
				tempArrayList.add(tempResult);
			}
			System.out.println("======"+count+"======");
			rset.close();
			stmt.close();
			con.close();
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
			System.out.println(sqle.getMessage());
		}
		return tempArrayList;
	}
	/* 
	 * 때밀이또추가 업데이트 인설트문
	 */
	public int excuteUpdate(String sql)
	{
		System.out.println(sql);
		int result=0;
		
		try
		{
			Connection con = DBManager.getConnection();
			Statement stmt= con.createStatement();

			result = stmt.executeUpdate(sql);

			
			stmt.close();
			con.close();
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
			System.out.println(sqle.getMessage());
		}
		
		return result;
	}
	

	
	
}